package com.yuma.tingshu.album.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuma.tingshu.album.cache.YuMaCache;
import com.yuma.tingshu.album.service.TrackInfoService;
import com.yuma.tingshu.album.service.VodService;
import com.yuma.tingshu.common.annotation.YuMaLogin;
import com.yuma.tingshu.common.constant.RedisConstant;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.common.util.AuthContextHolder;
import com.yuma.tingshu.model.album.TrackInfo;
import com.yuma.tingshu.query.album.TrackInfoQuery;
import com.yuma.tingshu.vo.album.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jodd.util.ThreadUtil;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    /**
     * 上传声音
     *
     * @param file
     * @return
     */
    @PostMapping("uploadTrack")
    public Result<Object> uploadTrack(MultipartFile file) {
        Object obj = this.vodService.uploadTrack(file);
        return Result.ok(obj);
    }

    /**
     * 声音新增
     *
     * @param trackInfoVo
     * @return
     */
    @YuMaLogin
    @PostMapping("saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    /**
     * 根据id查询声音
     *
     * @param trackId
     * @return
     */
    @GetMapping("getTrackInfo/{trackId}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = this.trackInfoService.getTrackInfoById(trackId);
        return Result.ok(trackInfo);
    }

    /**
     * 查看声音专辑列表
     *
     * @param page
     * @param limit
     * @param query
     * @return
     */
    @YuMaLogin
    @PostMapping("findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Integer page,
                                                       @PathVariable Integer limit,
                                                       @RequestBody TrackInfoQuery query) {

        Long userId = AuthContextHolder.getUserId();
        query.setUserId(userId == null ? 1 : userId);
        Page<TrackListVo> trackListVoPage = this.trackInfoService.findUserTrackPage(page, limit, query);
        return Result.ok(trackListVoPage);
    }

    /**
     * 保存修改声音数据
     *
     * @param trackId
     * @param trackInfo
     * @return
     */
    @PutMapping("updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId,
                                  @RequestBody TrackInfo trackInfo) {
        trackInfo.setId(trackId);
        trackInfoService.updateTrackInfo(trackId, trackInfo);
        return Result.ok();
    }

    /**
     * 删除声音
     *
     * @param trackId
     * @return
     */
    @DeleteMapping("removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        this.trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    /**
     * 根据专辑id分页查询声音
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @YuMaLogin(required = false)
    @GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
        Page<AlbumTrackListVo> albumTrackListVoPage = this.trackInfoService.findAlbumTrackPage(albumId, page, limit);
        return Result.ok(albumTrackListVoPage);
    }

    /**
     * 根据id查询声音统计数据
     *
     * @param trackId
     * @return
     */
    @GetMapping("getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = this.trackInfoService.getBytrackId(trackId);
        return Result.ok(trackStatVo);
    }

    /**
     * 查询用户购买过声音
     * @param trackId
     * @return
     */
    @YuMaLogin
    @GetMapping("findUserTrackPaidList/{trackId}")
    public Result<List<TrackOrderVo>> findUserTrackPaidList(@PathVariable Long trackId) {
        List<TrackOrderVo> trackOrderVos = this.trackInfoService.findUserTrackPaidList(trackId);
        return Result.ok(trackOrderVos);
    }

    /**
     * 获取声音交易列表
     * @param trackId
     * @param tradeCount
     * @return
     */
    @YuMaLogin
    @GetMapping("findTradeTrackList/{trackId}/{tradeCount}")
    public Result<List<TrackInfo>> findTradeTrackList(@PathVariable("trackId") Long trackId, @PathVariable("tradeCount") Integer tradeCount) {
        List<TrackInfo> paidtrackInfos = this.trackInfoService.findTradeTrackList(trackId, tradeCount);
        return Result.ok(paidtrackInfos);
    }

}

